package com.xingtai.util;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
import java.util.ResourceBundle;

public class Utilities {

	public static Properties loadParams(String file) throws IOException {

		// Loads a ResourceBundle and creates Properties from it
		Properties prop = new Properties();
		ResourceBundle bundle = ResourceBundle.getBundle(file);

		String key = null;
		for (Enumeration enuma = bundle.getKeys(); enuma.hasMoreElements();) {
			key = (String) enuma.nextElement();
			prop.put(key, bundle.getObject(key));
		}
		return prop;
	}

	/**
	 * This method convert byte array to String
	 * 
	 * @author rocky
	 * @return String
	 * @param byte[] b,int bLen is :b' availability length
	 */
	public static String ByteArraytoHexString(byte[] b, int bLen) {
		int iLen = bLen;
		// 每个byte用两个字符才能表示，所以字符串的长度是数组长度的两倍
		StringBuffer sb = new StringBuffer(iLen * 2);
		for (int i = 0; i < iLen; i++) {
			int intTmp = b[i];
			// 把负数转换为正数
			while (intTmp < 0) {
				intTmp = intTmp + 256;
			}
			// 小于0F的数需要在前面补0
			if (intTmp < 16) {
				sb.append("0");
			}
			sb.append(Integer.toString(intTmp, 16));
		}
		return sb.toString();
	}

	/**
	 * This method convert whole byte array to String
	 * 
	 * @param b
	 *            byte[]
	 * @return String
	 * @author rocky
	 */
	public static String ByteArraytoHexString(byte[] b) {
		return ByteArraytoHexString(b, b.length);
	}

}